package com.nuoniu.sibanyun.controller.erp;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.*;
import com.nuoniu.sibanyun.entity.erp.ErpVoucherDesign;
import com.nuoniu.sibanyun.entity.vo.VoucherReferenceVo;
import com.nuoniu.sibanyun.service.erp.IErpVoucherDesignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 单据设计 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-23
 */
@RestController
@RequestMapping("/erp/voucherDesign")
@Api(value = "单据设计", tags = "单据设计")
@Log4j2
public class ErpVoucherDesignController extends BaseController {

    @Autowired
    private IErpVoucherDesignService erpVoucherDesignService;

    @PostMapping("/voucherDynamicPage")
    @ApiOperation(value = "某个表单的动态分页查询")
    public HttpResult<Page<HashMap<String,Object>>> voucherDynamicPage(@RequestBody DynamicVoucherDto dynamicVoucherDto) {
        dynamicVoucherDto.setIsSelectCreateData(null);
        dynamicVoucherDto.setInWarehouseIds(null);
        return HttpResult.ok(erpVoucherDesignService.voucherDynamicPage(dynamicVoucherDto));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取某个表单的 当前配置")
    public HttpResult<List<ErpVoucherDesign>> list(ErpVoucherDesignSearchDto erpVoucherDesignSearchDto) {
        return HttpResult.ok(erpVoucherDesignService.getCurrentErpVoucherDesign(erpVoucherDesignSearchDto));
    }

    @GetMapping("/voucherDynamicById")
    @ApiOperation(value = "通过ID获取某个单据的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="source",value="单据类型",required=true,paramType="query"),
            @ApiImplicitParam(name="currentId",value="当前页面ID",required=false,paramType="query"),
            @ApiImplicitParam(name="actionType",value="当前动作 1:上一页 2：下一页 3：首页 4：尾页",required=false,paramType="query"),
    })
    public HttpResult list(DynamicVoucherDetailDto dynamicVoucherDetailDto) {
        return HttpResult.ok(erpVoucherDesignService.voucherDynamicById(dynamicVoucherDetailDto));
    }

    @PostMapping("/save")
    @ApiOperation(value = "批量修改 表单设计")
    public HttpResult save(@RequestBody List<ErpVoucherDesignDto> erpDocumentDesignDtos) {
        erpVoucherDesignService.batchUpdate(erpDocumentDesignDtos);
        return HttpResult.ok();
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增自定义字段")
    public HttpResult add(@RequestBody ErpVoucherDesignAddDto erpVoucherDesignAddDto) {
        erpVoucherDesignService.addCustomField(erpVoucherDesignAddDto);
        return HttpResult.ok();
    }

    @GetMapping("/listByReferenceTableCode")
    @ApiOperation(value = " 通过单据唯一code查询 关联的引用属性")
    public HttpResult<List<VoucherReferenceVo>> listByReferenceTableCode(String source) {
        return HttpResult.ok(erpVoucherDesignService.listByReferenceTableCode(source));
    }
    @PostMapping("/addProperty")
    @ApiOperation(value = "新增引用属性")
    public HttpResult addReferencePropertyField(@RequestBody VoucherReferenceVo voucherReferenceVo){
        erpVoucherDesignService.addReferencePropertyField(voucherReferenceVo);
        return HttpResult.ok();
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "通过Id删除")
    public HttpResult delete(Integer id){
        erpVoucherDesignService.deleteCustomField(id);
        return HttpResult.ok();
    }

    /*********************** 超级管理员权限 *******************************/
    @GetMapping("/listDef")
    @ApiOperation(value = "获取 系统单据设计(超级管理员权限)")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult<List<ErpVoucherDesign>> listDef(String source) {
        return HttpResult.ok(erpVoucherDesignService.getDefErpVoucherDesign(source));
    }

    @PostMapping("/saveDef")
    @ApiOperation(value = "批量修改 系统表单设计(超级管理员权限)")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult saveDef(@RequestBody List<ErpVoucherDesignDto> erpDocumentDesignDtos) {
        erpVoucherDesignService.batchSystemUpdate(erpDocumentDesignDtos);
        return HttpResult.ok();
    }

    @PostMapping("/enableSystemField")
    @ApiOperation(value = "启用 单据设计字段 (超级管理员权限)")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult enableSystemField(Integer id) {
        erpVoucherDesignService.enableSystemField(id);
        return HttpResult.ok();
    }

    @PostMapping("/disableSystemField")
    @ApiOperation(value = "禁用 单据设计字段 (超级管理员权限)")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult disableSystemField(Integer id) {
        erpVoucherDesignService.disableSystemField(id);
        return HttpResult.ok();
    }

    @PostMapping("/deleteSystemField")
    @ApiOperation(value = "删除 单据设计字段 (超级管理员权限)")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult deleteSystemField(Integer id) {
        erpVoucherDesignService.deleteSystemField(id);
        return HttpResult.ok();
    }

    @PostMapping("/lockField")
    @ApiOperation(value = "锁定 单据设计字段 (超级管理员权限)")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult lockField(ErpVoucherDesignDto erpVoucherDesignDto) {
        erpVoucherDesignService.lockField(erpVoucherDesignDto);
        return HttpResult.ok();
    }

    @PostMapping("/unLockField")
    @ApiOperation(value = "解锁 单据设计字段 (超级管理员权限)")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult lockField(Integer id) {
        erpVoucherDesignService.unLockField(id);
        return HttpResult.ok();
    }




//    @GetMapping("/initDataFromSqlServer")
//    @ApiOperation(value = "转换T+ 单据设置 字段")
//    public HttpResult initDataFromSqlServer() {
//        log.info("转换T+ 单据设置 字段 开始》》》》》》》》》》》》》》》》》》》》》》");
//        List<ErpVoucherType> erpVoucherTypes = erpVoucherTypeService.listDefErpVoucherType();
//        int size = 20;
//        int total = erpVoucherTypes.size();
//        int pageSize = total / size;
//        int model = total % size;
//        pageSize += model > 0 ? 1:0;
//
//        log.info("total:{}pageSize:{}",total,pageSize);
//        for(int i = 0;i<pageSize; i ++){
//            int start = i * size;
//            int end = (i + 1) * size ;
//            if(end > total  ){
//                end = total ;
//            }
//            List<ErpVoucherType> erpVoucherTypeList = erpVoucherTypes.subList(start, end);
//            log.info("下标：i:{}",i);
//            erpVoucherDesignService.ansyncFun(erpVoucherTypeList);
//        }
//        log.info("转换T+ 单据设置 字段 结束《《《《《《《《《《《《《《《《《《《《《《《《《");
//        return HttpResult.ok();
//    }


}
